<template>
	<div class="userBg">
		<div class="container">
			<u-subsection :list="subs" :current="tabIndex" @change="changTabs"></u-subsection>
			<div class="userBox"  v-if="showList.length > 0">
				<div class="list" v-if="tabIndex == 0" >
					<div class="item" v-for="(item,index) in showList" :key="index">
						<h3>{{ item.name }}</h3>
						<div class="time"><div class="label">请假时间：</div>{{ item.startTime }}</div>
						<div class="time"><div class="label">请假时间：</div>{{ item.endTime }}</div>
						<div class="reason">{{ item.reason }}</div>
						<div class="btn">
							<u-button type="primary" text="通过"  @click="agree(item)"></u-button>
						</div>
					</div>
				</div>
				<div class="list" v-else>
					<div class="item" v-for="(item,index) in showList" :key="index">
						<h3>{{ item.name }}</h3>
						<div class="time"><div class="label">请假时间：</div>{{ item.startTime }}</div>
						<div class="time"><div class="label">请假时间：</div>{{ item.endTime }}</div>
						<div class="time" v-if="item.spTime"><div class="label">审批时间：</div>{{ item.spTime ? item.spTime : '' }}</div>
						<div class="reason">{{ item.reason }}</div>
					</div>
					
				</div>
			</div>
			<div class="userBox" v-else>
				<u-empty
				        mode="list"
				        icon="http://cdn.uviewui.com/uview/empty/list.png"
				>
				</u-empty>
			
			</div>
		</div>
		
		<div class="loadingBox" v-if="showLoading">
			<image src="https://www.baixuemianshi.com/zhuanti/tingkezheng/images/loading.gif" mode="widthFix"></image>
		</div>
		
		
	</div>
</template>

<script>
	import {
		mapActions
	} from 'vuex';
	const db = wx.cloud.database()
	export default {
		data() {
			return {
				students:[],
				banji:"",
				subs:['未审核','已审核'],
				tabIndex:0,
				allQj:[],
				qjList:[],
				showLoading:true
			}
		},
		onLoad(options) {
			this.banji = options.banji
			this.getStudents(0)
		},
		onReady() {
			
		},
		computed: {
			userphone() {
				return this.$store.getters.userphone ? this.$store.getters.userphone : '';
			},
			showList(){
				let that = this
				
				let list = that.qjList.filter(item => item.status == that.subs[that.tabIndex])
				console.log(list, that.qjList,that.subs[that.tabIndex])
				return list
			}
		},
		created() {
			let that = this;
		},
		mounted() {
			let that = this 
			
		},
		watch: {
			
		},
		methods: {
			...mapActions(['LogSetPhone','LogSetOpenid']),
			
			getopenid() {
				let that = this
				wx.cloud.callFunction({
					name: 'getopenid',
					data: {},
					complete: res => {
						wx.setStorageSync("useropenid", res.result.openid)
						that.openid = res.result.openid
					}
				})
			},
			getStudents(page) {
				let that = this
				wx.cloud.callFunction({
					name: 'get_page_banji',
					data: {
						fncname: "nbhd_tiku_students",
						banji: that.banji,
						page: page,
						limit: 100
					},
					success: res => {
						if(res.result.data.length > 0){
							that.students = that.students.concat(res.result.data)
							let p = page + 1
							that.getStudents(p)
						}else{
							that.getallQj(0)
						}
					},
					fail: req => {
						console.log(req)
					}
				});
			},
			getallQj(page){
				let that = this
				wx.cloud.callFunction({
					name: 'get_page',
					data: {
						fncname: "24gk_qingjia",
						page: page,
						limit: 100
					},
					success: res => {
						if(res.result.data.length > 0){
							that.allQj = that.allQj.concat(res.result.data)
							let p = page + 1
							that.getallQj(p)
						}else{
							that.filterStudent()
						}
						
					},
					fail: req => {
						console.log(req)
					}
				});
			},
			filterStudent(){
				let that = this
				let a = []
				that.allQj.map((item,index) => {
					that.students.map((s,j) => {
						if(item.phone == s.phone){
							a.push(item)
						}
					})
				})
				that.qjList = a
				that.showLoading = false
			},
			getTime(){
				let that = this
				let day = new Date()
				let y = day.getFullYear()
				let m = (day.getMonth() + 1).toString().padStart(2, '0');
				let d = day.getDate().toString().padStart(2, '0');
				let hour = day.getHours().toString().padStart(2, '0');
				let miu = day.getMinutes().toString().padStart(2, '0');
				let time = y + '-' + m + '-' + d + ' ' + hour + ':' + miu
				return time
			},
			agree(info){
				let that = this
				that.showLoading = true
				db.collection('24gk_qingjia').doc(info._id).update({
					data:{
						status: '已审核',
						spTime: that.getTime()
					},
					success:function(){
						that.students = []
						that.allQj = []
						that.getStudents(0)
					}
				})
			},
			changTabs(e){
				this.tabIndex = e
			}
		},
		components: {
			
		}
	};
</script>

<style scoped>
	@import url(./index.css);
</style>